﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Printing;
namespace carApp
{
    public partial class Prints : Form
    {
        public Prints()
        {
            InitializeComponent();
        }
        string OldActPrn = string.Empty;
        private void Prints_Load(object sender, EventArgs e)
        {
            int i;
            string pkInstalledPrinters;
            using (PrintDocument pd = new PrintDocument())
            {
                for (i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)  //开始遍历
                {
                    pkInstalledPrinters = PrinterSettings.InstalledPrinters[i];  //取得名称
                    cmbPrinterList.Items.Add(pkInstalledPrinters);               //加入ComboBox
                    if (pd.PrinterSettings.IsDefaultPrinter)                     //判断是否为默认打印机
                    {
                        OldActPrn = pd.PrinterSettings.PrinterName;              //保存名称，后面要用
                        cmbPrinterList.Text = pd.PrinterSettings.PrinterName;    //显示默认打印机名称
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Printer.ChangeDefaultPrinter(cmbPrinterList.Text);
        }
        static int pageHeight = 395;//单张375
        static int pageWidth = 756;
        static int pageNum = 1;
        static int totalHeight = 0;
        private static void print()
        {
            System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
            System.Drawing.Printing.Margins margin = new System.Drawing.Printing.Margins(0, 0, 0, 0);
            pd.DefaultPageSettings.Margins = margin;
            totalHeight = pageHeight * pageNum;
            System.Drawing.Printing.PaperSize pageSize = new System.Drawing.Printing.PaperSize("print_ticket", pageWidth, totalHeight);
            pd.DefaultPageSettings.PaperSize = pageSize;
            pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(pd_PrintPage);
            //pd.PrinterSettings.PrinterName = "";//指定打印机
            PrintPreviewDialog ppd = new PrintPreviewDialog();
            ppd.Document = pd;
            ppd.ShowDialog();
            try
            {
                //pd.Print();
            }
            catch (Exception ex)
            {
                pd.PrintController.OnEndPrint(pd, new System.Drawing.Printing.PrintEventArgs());
            }
        }
        private static void pd_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            try
            {
                string pr_string = "";               
                int line_height = 24;
                int line_margin = 8;
                Graphics g = e.Graphics;
                Font ft = new Font("宋体", 8F, FontStyle.Regular);
                #region drawing
                for (int i = 0; i < pageNum; i++)
                {                  
                    g.DrawString("-", new Font("宋体", 2F, FontStyle.Regular), new SolidBrush(Color.White), new Point(i * pageHeight, 1));
                   
                    g.DrawString("-", ft, new SolidBrush(Color.White), new Point(i * pageHeight, totalHeight - 5));
                }
                g.Dispose();
                #endregion
                pr_string = "";
            }
            catch (Exception ex)
            {
                ReportEx.upPortEX(ex.ToString());
            }
        }
    }

    static class Printer
    {
        /// <summary>
        /// 设置默认的打印机
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [System.Runtime.InteropServices.DllImport("winspool.drv", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
        private static extern Boolean SetDefaultPrinter(string name);
        /// <summary>
        /// 设置默认的打印机,如果失败抛出异常.
        /// </summary>
        /// <param name="name"></param>
        public static void ChangeDefaultPrinter(string name)
        {
            var result = SetDefaultPrinter(name);
            if (!result)
                throw new System.ComponentModel.Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
        }
    }
}
